﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using Microsoft.Xna.Framework.Graphics;
using Microsoft.Xna.Framework;

namespace ShootEm.Components
{
    class Explosion : Microsoft.Xna.Framework.DrawableGameComponent
    {
        protected Texture2D texture;
        public int x;
        public int y;
        float timer = 0f;
        //The interval (100 milliseconds)
        float interval = 30f;
        int spriteWidth = 64;
        int spriteHeight = 64;
        int currentFrame = 1;
        Rectangle sourceRect;
        //The centre of the current 'frame'
        Vector2 origin;
        public Explosion(ShootEm game, int x, int y)
            : base(game)
        {
            this.x = x;
            this.y = y;
        }
        public override void Initialize()
        {
            // TODO: Add your initialization code here
            this.texture = this.Game.Content.Load<Texture2D>("Textures/explosion");
            base.Initialize();
        }
        public override void Update(GameTime gameTime)
        {
            // The avatar follows the mouse position
            /* MouseState mouseState = Mouse.GetState();
             this._x = mouseState.X;
             this._y = mouseState.Y;*/
            this.iterationOnTexture(gameTime);
            base.Update(gameTime);
        }
        private void iterationOnTexture(GameTime gameTime)
        {
            //Increase the timer by the number of milliseconds since update was last called
            timer += (float)gameTime.ElapsedGameTime.TotalMilliseconds;

            //Check the timer is more than the chosen interval
            if (timer > interval)
            {
                //Show the next frame
                currentFrame++;
                //Reset the timer
                timer = 0f;
            }

            if (currentFrame == 16)
            {
                this.Game.Components.Remove(this);
            }
            else
            {
                int col, row;
                col = currentFrame / 4;
                row = currentFrame / 4;
                sourceRect = new Rectangle(col * spriteWidth, row*spriteHeight, spriteWidth, spriteHeight);
                origin = new Vector2(sourceRect.Width / 2, sourceRect.Height / 2);
            }

            
        }
        public override void Draw(GameTime gameTime)
        {



            ((ShootEm)this.Game).spriteBatch.Begin();
            ((ShootEm)this.Game).spriteBatch.Draw(this.texture, new Vector2(this.x, this.y), sourceRect, Color.White, 0f, origin, 1, SpriteEffects.None, 0);
            ((ShootEm)this.Game).spriteBatch.End();
            base.Draw(gameTime);
        }

    }
}
